<template>
  <div>
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">添加管理员</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'last_time'">
          {{ dayjs.unix(record.last_time).format('YYYY-MM-DD hh:mm:ss') }}
          <!-- {{ dayjs(record.last_time * 1000).format('YYYY-MM-DD hh:mm:ss') }} -->
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:info-standard-line',
                tooltip: '查看用户详情',
                auth: 'system-log',
                onClick: handleView.bind(null, record),
              },
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑用户资料',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除此账号',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AdminDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import dayjs from 'dayjs';
  import { defineComponent, reactive } from 'vue';
  import AdminDrawer from './AdminDrawer.vue';
  import { useDrawer } from '/@/components/Drawer';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getAdminList, delAdmin } from '/@/api/system/admin';
  import { columns, searchFormSchema } from './admin.data';
  import { useGo } from '/@/hooks/web/usePage';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'AdminManagement',
    components: { BasicTable, TableAction, AdminDrawer },
    setup() {
      const go = useGo();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const searchInfo = reactive<Recordable>({});
      const [registerTable, { reload, updateTableDataRecord }] = useTable({
        title: '管理员列表',
        api: getAdminList,
        rowKey: 'id',
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
          autoSubmitOnEnter: true,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        // afterFetch(e) {
        //   console.log(e, 'jjjj');
        //   return e.map((item: any) => {
        //     item.last_time = item.last_time * 1000;
        //     return item;
        //   });
        // },
        handleSearchInfoFn(info) {
          return info;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
        },
      });

      const { createMessage } = useMessage();
      // 创建
      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }
      // 修改
      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }
      // 删除
      function handleDelete(record: Recordable) {
        delAdmin(record.id).then(() => {
          createMessage.success('删除成功');
          reload();
        });
      }

      function handleSuccess({ isUpdate, values }) {
        if (isUpdate) {
          // 演示不刷新表格直接更新内部数据。
          // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
          const result = updateTableDataRecord(values.id, values);
          console.log(result);
        } else {
          reload();
        }
      }

      function handleSelect(deptId = '') {
        searchInfo.deptId = deptId;
        reload();
      }
      // 详情
      function handleView(record: Recordable) {
        go('/system/admin/admin_detail/' + record.id);
      }

      return {
        dayjs,
        registerDrawer,
        registerTable,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleSelect,
        handleView,
        searchInfo,
      };
    },
  });
</script>
